package com.myz.service.hystrix.service.impl;

import com.myz.service.hystrix.service.IHystrixRedisCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author maoyz
 */
@CacheConfig(cacheNames = {"com.myz.service.hystrix.service.impl"})
@Service
public class HystrixRedisCacheServiceImpl implements IHystrixRedisCacheService {

    private static final Logger LOGGER = LoggerFactory.getLogger(HystrixRedisCacheServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Cacheable(key = "'product.' + #id")
    @Override
    public String cacheGet(Integer id) {
        LOGGER.info("****************** cacheGet id = {} *******************", id);
        ParameterizedTypeReference<String> type = new ParameterizedTypeReference<String>() {
        };
        ResponseEntity<String> responseEntity = restTemplate.exchange("http://SERVICE-MEMBER/member/index", HttpMethod.GET, null, type);
        LOGGER.info("************** responseEntity = {} ***************", responseEntity);
        return responseEntity.getBody();
    }

    @CacheEvict(key = "'product.' + #id")
    @Override
    public String cacheDel(Integer id) {
        LOGGER.info("****************** cacheDel id = {} *******************", id);
        return null;
    }
}
